<%@ page session="true"%>
<%
	String canVote = (String)session.getAttribute("canVote");
%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<link rel="stylesheet" href="http://m.ex.co.kr:8090/vote/images/jquery.alerts.css" />
<script src="http://m.ex.co.kr:8090/vote/js/jquery.alerts.js"></script>
<script type="text/javascript" src="./js/aes/aes-enc.js"></script> 
<script type="text/javascript" src="./js/aes/aes-dec.js"></script> 
<script type="text/javascript" src="./js/aes/aes-test.js"></script> 
<script language="javascript">

var mobileType = "";

function encode64(str){
 return encode(escape(str));
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 
function encode(input){
 var output = "";
 var chr1, chr2, chr3;
 var enc1, enc2, enc3, enc4;
 var i =0;
 
 do{
  chr1 = input.charCodeAt(i++);
  chr2 = input.charCodeAt(i++);
  chr3 = input.charCodeAt(i++);
 
  enc1 = chr1 >> 2;
  enc2 = ((chr1 & 3) << 4) | (chr2 >> 4 );
  enc3 = ((chr2 & 15) << 2) | (chr3 >> 6 );
  enc4 = chr3 & 63;
 
  if(isNaN(chr2)){
   enc3 = enc4 =64;
  }else if(isNaN(chr3)){
   enc4 = 64;
  }
 
  output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
 }while(i<input.length);
 
 return output;
}

function onSuccess(data, status){
	var userId = $("#text_swbeonho").attr("value");
	
	var uAgent = navigator.userAgent.toLowerCase();
	var belowICS = false;

	if (uAgent.indexOf("2.1.") > -1 || uAgent.indexOf("2.2.") > -1 || uAgent.indexOf("2.3.") > -1){
		belowICS = true;
	}
	
	if($.trim(data) == "success"){
		if(mobileType == "android"){
			//alert("below ICS : "+belowICS);
			if(belowICS){
				document.location.replace("http://m.ex.co.kr:8090/vote/voteelementlist_except.jsp?key=4&userId="+userId);		
			}else{
				document.location.replace("http://m.ex.co.kr:8090/vote/voteelementlist_param.jsp?key=4&userId="+userId);		
			}
		}else{
			document.location.replace("http://m.ex.co.kr:8090/vote/voteelementlist_param.jsp?key=4&userId="+userId);
		}
		
		//location.href="http://m.ex.co.kr:8090/vote/voteelementlist.jsp?key=1&userId="+userId
	}else{
		$("#text_password").text("");
		
		//jAlert(data, "확인");
		alert(data);
	}
}

function onError(data, status){
	//jAlert("서버 요청 중 오류가 발생하였습니다."+data, "확인");
	alert("서버 요청 중 오류가 발생하였습니다."+data);
}

function init(){
<%
	//session.removeAttribute("loginCheck");
%>	
	var uAgent = navigator.userAgent.toLowerCase();
	var mobilePhones = new Array('iphone', 'ipod', 'ipad', 'android', 'blackberry', 'windows ce', 'nokia', 'webos', 'opera mini', 'sonyericsson', 'opera mobi', 'iemobile');   //기기종류 나열
	var isMobile = "N";
	 
	for (var i = 0; i < mobilePhones.length; i++) {
	    if (uAgent.indexOf(mobilePhones[i]) > -1){
	    	isMobile = "Y";
	    	mobileType = mobilePhones[i];
	    }
	}// for문 끝	
				
	if(isMobile == "N"){
		//jAlert("모바일 단말에서만 투표하실 수 있습니다.", "확인");
		alert("모바일 단말에서만 투표하실 수 있습니다.");
		history.back();
		return;	
	}	
	
//	if("<%=canVote%>" == "false"){
//		alert("이미 투표하신 휴대폰입니다.");
//		history.back();
//		return;	
//	}			
	
	var width = top.window.innerWidth;
	var height = top.window.innerHeight;
	var offsetWidth = loginForm.offsetWidth;
	var offsetHeight = loginForm.offsetHeight;
	var offsetWidthId = label_swbeonho.offsetWidth;
	var offsetHeightId = label_swbeonho.offsetHeight;	
	var offsetWidthPwd = label_password.offsetWidth;
	var offsetHeightPwd = label_password.offsetHeight;			
	
	var screenMarginHeight = top.window.innerHeight / 10;
	var screenMarginWidth = top.window.innerWidth / 10;
		
	if(height <= 300){
		height = height * 2;
	}
		
  divContent.style.height = height + "px";
  loginForm.style.top = height - offsetHeight - screenMarginHeight + "px";
  loginForm.style.width = width - (screenMarginWidth * 2) + "px";
  loginForm.style.left = (width - offsetWidth) / 2 + "px";
  text_swbeonho.style.width = offsetWidth - offsetWidthId - screenMarginWidth + "px";
  text_password.style.width = offsetWidth - offsetWidthPwd - screenMarginWidth + "px";

	orientationChg();

	//setTimeout(function() { window.scrollTo(0, 1); }, 100); //to hide the address bar
}

function orientationChg(){
	var angle = window.orientation;
	var orientation = "portrait";
	
	if(angle > 0){
		orientation = "landscape";
	}	

	var uAgent = navigator.userAgent.toLowerCase();
	var isTablet = false;
	
	if (uAgent.indexOf("m380") > -1){
		if(orientation == "portrait"){
			divContent.style.backgroundImage = "url(../vote/images/main_bg_1280.jpg)";
		}else{
			divContent.style.backgroundImage = "url(../vote/images/main_bg_720.jpg)";
		}
	}else{
		if(orientation == "landscape"){
			divContent.style.backgroundImage = "url(../vote/images/main_bg_1280.jpg)";
		}else{
			if (uAgent.indexOf("iphone") > -1){
				divContent.style.backgroundImage = "url(../vote/images/main_bg_480.jpg)";
			}else{			
				divContent.style.backgroundImage = "url(../vote/images/main_bg_720.jpg)";
			}
		}		
	}

	
	//window.scrollTo(0, 1);	
}

window.addEventListener('orientationchange', function(){orientationChg();}, false); 
//document.addEventListener('DOMContentLoaded', function () { setTimeout(init, 2000); }, false);
//window.addEventListener('load', setTimeout(function () { init(); }, 200), false);	

$(document).ready(function(){
	$("#submit").click(function(){
		
		if($.trim($("#text_swbeonho").attr("value")) == ""){
			//jAlert("사원번호를 입력하여 주십시오.", "확인");
			alert("사원번호를 입력하여 주십시오.");
			$("#text_swbeonho").focus();
			return false;
		}
		
		if($.trim($("#text_password").attr("value")) == ""){
			//jAlert("하이포탈 비밀번호를 입력하여 주십시오.", "확인");
			alert("하이포탈 비밀번호를 입력하여 주십시오.");
			$("#text_password").focus();
			return false;
		}		
		
		var plainId = $.trim($("#text_swbeonho").attr("value"))		
		var plainPwd = $.trim($("#text_password").attr("value"))		
		var encId = encode64(plainId);
		var encPwd = encode64(plainPwd);
				
		$.ajax({
			type:"POST",
			url:"./index_action.jsp",
			cache:false,
			data:"encId="+encId+"&encPwd="+encPwd,
			success:onSuccess,
			error:onError
		});
		
		return false;
	});
});

</script>
<style type="text/css"> 	
.inlineText{display:inline !important;}
.inputText{!important;}
body{
-webkit-text-size-adjust:none; min-height:200px;}
body[orient="portrait"]{min-height:200px;}
body[orient="landscape"]{min-height:200px;}
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" style="width:100%; height:100%;">
<form>
<input type="hidden" id="hiddenEncText" value="123">
<div id="divContent" style="width:100%;background-image:url(./images/main_bg_720.jpg) ;background-position:center;background-size:cover;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;">
		<div id="loginForm" data-role="controlgroup" style="position:absolute;background-color:rgba(247,247,247,0.7);padding:10px">
			<div>
				<label id="label_swbeonho" class="inlineText"><strong style="color:#383838">사원번호 : </strong></label>
				<input id="text_swbeonho" type="tel" class="inlineText inputText" max=8 placeholder="사원번호" />	
			</div>
			<div>				
				<label id="label_password" class="inlineText"><strong style="color:#383838">비밀번호 : </strong></label>
				<input type="password" id="text_password" class="inlineText inputText" placeholder="하이포탈 비밀번호"/>	
			</div>
			<div>
				<button type="submit" data-theme="b" id="submit">로그인</button>
			</div>			
		</div>
</div>
</form>
</body>
</html>
<script language="javascript">	
$(document).on('pageinit', function () {
  setTimeout(init, 200);
});
</script>